package com.ljx.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.Domain.Category;
import com.ljx.Domain.Dish;
import com.ljx.Domain.Setmeal;
import com.ljx.Exception.AssociatedException;
import com.ljx.Mapper.CategoryMapper;
import com.ljx.Service.CategoryService;
import com.ljx.Service.DishService;
import com.ljx.Service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    /**
     * 删除菜单
     */
    @Override
    public void Delete(Long id) {
        //查找菜单类，判断该类是否关联菜单类别id
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(wrapper);
        if(count >0){
            //说明关联菜单类，不能删除
            throw new AssociatedException("已有菜单关联，删除失败！");
        }
        //查找套餐类，判断是否关联菜单类别id
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(wrapper1);
        if(count >0){
            //说明关联套餐类，不能删除
           throw  new AssociatedException("已有套餐关联，删除失败！");
        }
        super.removeById(id);
    }
}
